<template>
  <com-card>
    <div slot="com_header">商品列表</div>
    <div slot="com_con">
      <!-- 条件查询 -->
      <el-row>
        <el-form :model="tableObj" :inline="true">
          <el-form-item label="商品名称">
            <el-input type="text" v-model="tableObj.name"></el-input>
          </el-form-item>
          <el-form-item label="商品分类">
            <el-input type="text" v-model="tableObj.category"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="query">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-row>

      <el-table :data="tableData" style="width: 100%" v-loading="loa" >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID:">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="所属分类:">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="商品图片:">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="商品评价:">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品描述:">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
              <el-form-item label="商品名称:">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="商品价格:">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="创建时间:">
                <span>{{ props.row.ctime }}</span>
              </el-form-item>
              <el-form-item label="商品销量:">
                <span>{{ props.row.sellCount }}</span>
              </el-form-item>
              <!-- <el-table-item label="操作">
                <el-button type="warning">编辑</el-button>
                <el-button type="danger" @click="del(props.row.id)"
                  >删除</el-button
                >
              </el-table-item> -->
            </el-form>
          </template>
        </el-table-column>

        <el-table-column label="商品名称" prop="name"> </el-table-column>
        <el-table-column label="所属分类" prop="category"> </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片">
          <template slot-scope="{ row }">
            <el-image :src="row.imgUrl"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="warning" @click="open(row)">编辑</el-button>
            <el-button type="danger" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- </el-table> -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tableObj.currentPage"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="tableObj.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="formObj">
          <el-form-item label="商品名称" :label-width="formLabelWidth">
            <el-input v-model="formObj.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="所属分类" :label-width="formLabelWidth">
            <el-select v-model="formObj.category" placeholder="请选择商品分类">
              <el-option
                v-for="(item, index) in this.alltype"
                :key="index"
                :value="item.cateName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格" :label-width="formLabelWidth">
            <el-input v-model="formObj.price" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="商品描述" :label-width="formLabelWidth">
            <el-input v-model="formObj.goodsDesc" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </com-card>
</template>

<script>
import ComCard from "@/components/card";
import {
  api_query,
  api_del,
  api_update,
  api_queryType,
} from "../../api/goods.js";
export default {
  components: {
    ComCard,
  },
  data() {
    return {
      loa:true,
      formLabelWidth: "120px",
      dialogFormVisible: false,
      tableData: [],
      total: 0,
      tableObj: {
        currentPage: 1,
        pageSize: 3,
        name: "",
        category: "",
      },
      formObj: "",
      alltype: [],
      imageUrl:'',
      srcurl:''
    };
  },
  mounted() {
    this.query();
    this.allfl();
  },
  methods: {
    //查询所有商品分类
    async allfl() {
      let rst = await api_queryType();
      let { categories } = rst;
      this.alltype = categories;
      console.log(this.alltype);
    },
    async query() {
     this.loa=true
      
      let rst = await api_query(this.tableObj);
      let { total, data } = rst;
      data = data.map((item) => {
        item.imgUrl = `http://localhost:5000/upload/imgs/goods_img/${item.imgUrl}`;
        return item;
      });
      this.tableData = data;
      this.total = total;
     this.loa=false
      console.log(this.tableData);
    },
    handleSizeChange(val) {
      this.tableObj.pageSize = val;
      this.query();
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.tableObj.currentPage = val;
      this.query();
      // console.log(`当前页: ${val}`);
    },
    async del(id) {
      let rst = await api_del({ id });
      this.query();
      console.log(rst);
    },
    open(row) {
      this.formObj = {...row};
       this.imageUrl=this.formObj.imgUrl
       // 截取获得图片名字
      let s = this.imageUrl.lastIndexOf('/')
      this.formObj.imgUrl= this.imageUrl.substring(s)
    
      this.dialogFormVisible = true;
    },
    async update() {
      let rst = await api_update(this.formObj);
      let { msg, code } = rst;
      if (code == 0) {
        this.$message.success(msg);
      }
      console.log(rst);
      this.query();
      this.dialogFormVisible = false;
    },


     handleAvatarSuccess(res, file) {
       this.formObj.imgUrl=res.imgUrl
       this.srcurl=res.imgUrl
        this.imageUrl = `http://localhost:5000/upload/imgs/goods_img/${res.imgUrl}`;
        console.log(res);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' ;
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
  },
};
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.demo-table-expand {
  padding-left: 130px;
}

 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>